//
//  FPPrevueViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPPrevueViewController.h"
#import "FPPrevueViewCell.h"
#import "FPNewsHeaderView.h"
#import "FPHeaderModel.h"
#import "FPPrevueModel.h"
#import "MoviePlayerViewController.h"
#import "FPHeadAanimation.h"

#define kPrevueListUrl @"http://api.m.mtime.cn/PageSubArea/TrailerList.api"
@interface FPPrevueViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)FPNewsHeaderView *headerView;
@property(nonatomic,strong)UITableView *tableView;
/**
 装prevueModel的数组
 */
@property(nonatomic,strong)NSMutableArray *prevueArray;
/**装在预告头部数组*/
@property(nonatomic,strong)NSMutableArray *pHeaderArray;

@end

@implementation FPPrevueViewController

- (FPNewsHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[FPNewsHeaderView alloc]init];
    }
    return _headerView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
    UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];

    self.headerView.frame =CGRectMake(0, 0, kScreenWidth, kImageH(150) + kImageH(30));
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHeaderImage)];
    [self.headerView.picImageView addGestureRecognizer:tap];
    [self.headerView.introButton addTarget:self action:@selector(clickHeaderImage) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = self.headerView;
    [self.tableView addGifHeaderWithRefreshingTarget:self  refreshingAction:@selector(prevueRefreshAction)];
    [self.tableView.header beginRefreshing];
   
   
}

- (void)clickHeaderImage
{
    MoviePlayerViewController *moveVc = [[MoviePlayerViewController alloc]init];
    FPHeaderModel *model = self.pHeaderArray[0];
    moveVc.mp4_url = model.mp4Url;
    [self.navigationController pushViewController:moveVc animated:YES];

   
}


- (void)prevueRefreshAction
{
    NSMutableArray *imageArray = [[NSMutableArray alloc]init];
    for (int i = 2; i< 10; i ++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"catRefresh－%d（被拖移）.tiff",i]];
        [imageArray addObject:image];
    }
    [self.tableView.gifHeader setImages:imageArray forState: MJRefreshHeaderStateIdle];
    [self.tableView.gifHeader setImages:imageArray forState:MJRefreshHeaderStatePulling];
    [self.tableView.gifHeader setImages:imageArray forState:MJRefreshHeaderStateRefreshing];
    [self analysisHeaderViewData];
    [self analysisPrevueList];
    [FPHeadAanimation analysisHA:self.tableView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark================解析顶部数据
- (void)analysisHeaderViewData
{
    
    if (![[FPNetWork shareInstance]rechability])
    {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:nil message:@"无网络" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [aler show];
    }
    self.pHeaderArray  = [NSMutableArray array];
    [LORequestManger GET:kHeaderURL success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *prevueDic = dic[@"trailer"];
        FPHeaderModel *prevueHeaerModel = [FPHeaderModel fpNewsViewModeltSetValueWithDictionary:prevueDic];
        [self.pHeaderArray addObject:prevueHeaerModel];
    //    NSLog(@"-------%@",prevueHeaerModel.mp4Url);
        [self.headerView.picImageView sd_setImageWithURL:[NSURL URLWithString:prevueHeaerModel.imageUrl] placeholderImage:nil completed:nil];
        [self.headerView.introButton setTitle:prevueHeaerModel.title forState:UIControlStateNormal];
        if ([prevueHeaerModel.type integerValue]== 1||!prevueHeaerModel.type )
        {
            self.headerView.rightImageView.hidden = NO;
        }
        else
        {
            self.headerView.rightImageView.hidden = YES;
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求数据失败");
    }];

}
#pragma mark ============解析Prevuelist
- (NSMutableArray *)prevueArray
{
    if (!_prevueArray) {
        _prevueArray = [[NSMutableArray alloc]init];
    }
    return _prevueArray;
}
- (void)analysisPrevueList
{
    if (![[FPNetWork shareInstance]rechability])
    {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:nil message:@"无网络" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [aler show];
    }
    [LORequestManger GET:kPrevueListUrl success:^(id response) {
    
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *trailerArray = dic[@"trailers"];
        for (NSDictionary *trailerDic in trailerArray) {
            FPPrevueModel *model = [FPPrevueModel fpPrevueModelSetValueWithDictionary:trailerDic];
         
            [self.prevueArray addObject:model];
        }
        //刷新表示图
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求网络失败");
    }];
    
}
#pragma mark ==============实现tableView的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.prevueArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"prevueCell";
    [tableView registerClass:[FPPrevueViewCell class] forCellReuseIdentifier:cellIndentifier];
    FPPrevueViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier forIndexPath:indexPath];
    //找到对应的模型
    FPPrevueModel *model = self.prevueArray[indexPath.row];
    [cell adjustSubviewsWithContent:model];
    return cell;
}
// 返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kImageH(80) + 2 * kImageH(10);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoviePlayerViewController *moveVc = [[MoviePlayerViewController alloc]init];
    FPPrevueModel *model = self.prevueArray[indexPath.row];
    moveVc.mp4_url = model.url;
  //  NSLog(@"==========%@",moveVc.mp4_url);
    [self.navigationController pushViewController:moveVc animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , 20)];
    statusBarView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:statusBarView];

    
}

@end
